package com.example.clxy.service.impl;

import com.example.clxy.dao.*;
import com.example.clxy.entity.Program;
import com.example.clxy.entity.Schedule;
import com.example.clxy.entity.ScheduleStudentGx;
import com.example.clxy.entity.User;
import com.example.clxy.entity.dto.ScheduleDto;
import com.example.clxy.entity.dto.Wrapper;
import com.example.clxy.entity.vo.ScheduleWlclassVo;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.*;
import com.example.clxy.result.business.PageResult.SchedulePageResult;
import com.example.clxy.result.business.PageResult.ScheduleRemindResult;
import com.example.clxy.result.business.PageResult.SemesterPageResult;
import com.example.clxy.service.ScheduleService;
import com.example.clxy.util.Tools;
import com.example.clxy.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.tools.Tool;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author liuhaoqi
 * @Date 2020/8/5 12:26
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    ScheduleMapper scheduleMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ScheduleStudentGxMapper scheduleStudentGxMapper;
    @Autowired
    WlclassMapper wlclassMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    ProgramMapper programMapper;
    @Autowired
    SemesterMapper semesterMapper;

    @Override
    public WebRestResult addScheduleByDataList(Wrapper<Schedule> scheduleWrapper) {
//        WebRestResult result = new WebRestResult();
//        List<Schedule> scheduleList = scheduleWrapper.getData();
//        for (int i = 0; i < scheduleList.size(); i++) {
//            for (int j = i + 1; j < scheduleList.size(); j++) {
//                if (scheduleList.get(i).getClassid().equals(scheduleList.get(j).getClassid()) &&
//                        scheduleList.get(i).getTeacherid().equals(scheduleList.get(j).getTeacherid())) {
//                    if (scheduleList.get(i).getExperimentid().equals(scheduleList.get(j).getExperimentid())
//                            && scheduleList.get(i).getStartdate().equals(scheduleList.get(j).getStartdate())
//                            && scheduleList.get(i).getEnddate().equals(scheduleList.get(j).getEnddate())
//                            && scheduleList.get(i).getSemesterid().equals(scheduleList.get(j).getSemesterid())
//                            && scheduleList.get(i).getExperimentroom().equals(scheduleList.get(j).getExperimentroom())) {
//                        result.setResult(WebRestResult.FAILURE);
//                        result.setMessage("排课有冲突，同一时间段同一地点只能老师不同或者班级不同！！");
//                        return result;
//                    }
//                }
//            }
//            if (scheduleList.get(i).getMaxnums() == null) {
//                scheduleList.get(i).setMaxnums(120);
//            }
//            if (scheduleList.get(i).getCurnums() == null) {
//                scheduleList.get(i).setCurnums(0);
//            }
//            scheduleList.get(i).setScheduleid(UUIDUtil.randomUUID());
//            scheduleList.get(i).setCreatedTime(new Date());
//            scheduleList.get(i).setUpdatedTime(new Date());
//            scheduleList.get(i).setFlag("0");
//        }
//        List<ScheduleStudentGx> scheduleStudentGxList = new ArrayList<>();
//        for (Schedule schedule : scheduleList) {
//            String classid = schedule.getClassid();
//            List<User> studentList = userMapper.getUserListByClassid(classid);
//            ScheduleStudentGx scheduleStudentGx = new ScheduleStudentGx();
//            if (studentList.size() > 0) {
//                for (int i = 0; i < studentList.size(); i++) {
//                    scheduleStudentGx.setStudentid(studentList.get(i).getUserid());
//                    scheduleStudentGx.setScheduleid(schedule.getScheduleid());
//                    scheduleStudentGx.setFlag("0");
//                    scheduleStudentGx.setCreatedTime(new Date());
//                    scheduleStudentGx.setUpdatedTime(new Date());
//                }
//                scheduleStudentGxList.add(scheduleStudentGx);
//            }
//        }
//        int flag = scheduleMapper.addSchedule(scheduleList);
//        int gxFlag = scheduleStudentGxMapper.addGxList(scheduleStudentGxList);
//        if (flag == scheduleList.size() && gxFlag == scheduleStudentGxList.size()) {
//            result.setResult(WebRestResult.SUCCESS);
//            result.setMessage("新增排课成功！");
//        } else {
//            result.setResult(WebRestResult.FAILURE);
//            result.setMessage("新增排课失败！");
//        }
//        return result;
        return null;
    }

    @Override
    public WebRestResult deleteSchedule(String scheduleid) {
        WebRestResult result = new WebRestResult();
        if (scheduleMapper.deleteSchedule(scheduleid) == 1 && scheduleStudentGxMapper.deleteScheduleStudentGx(scheduleid) > 0
                && scheduleMapper.deleteScheduleWlclass(scheduleid) > 0) {
            result.setMessage("删除排课成功");
            result.setResult(WebRestResult.SUCCESS);
        } else {
            result.setMessage("删除排课失败");
            result.setResult(WebRestResult.FAILURE);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

    @Override
    public WebRestResult updateSchedule(Schedule schedule) {
        WebRestResult result = new WebRestResult();
        if (scheduleMapper.updateByPrimaryKeySelective(schedule) == 1) {
            result.setMessage("更新排课成功");
            result.setResult(WebRestResult.SUCCESS);
        } else {
            result.setMessage("更新排课失败");
            result.setResult(WebRestResult.FAILURE);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

    @Override
    public PageResult<SchedulePageResult> getScheduleListByUser(String userid, String semesterid, String platformid, String experimentname, String wlclassid, String wlclassname, String grade, int page, int pagesize, String status) {
        PageResult<SchedulePageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        if (userMapper.selectByPrimaryKey(userid).getRoleid().equals("2")) {
            Date date = new Date();
            Timestamp timestamp = null;
            List<SchedulePageResult> list = null;
            if ("0".equals(status)) {
                list = scheduleMapper.getScheduleListBySemesteridTeacherN(userid, semesterid, platformid, experimentname, wlclassid, wlclassname, grade, begin, end, pagesize, timestamp);
                if (list.size() == 0) {
                    result.setMessage("没有排课信息！");
                    result.setResult(WebRestResult.FAILURE);
                    return result;
                }
            } else if ("1".equals(status)) {
                timestamp = new Timestamp(date.getTime());
                list = scheduleMapper.getScheduleListBySemesteridTeacherN(userid, semesterid, platformid, experimentname, wlclassid, wlclassname, grade, begin, end, pagesize, timestamp);
                if (list.size() == 0) {
                    result.setMessage("没有未上排课！");
                    result.setResult(WebRestResult.FAILURE);
                    return result;
                }
            } else if ("2".equals(status)) {
                timestamp = new Timestamp(date.getTime());
                list = scheduleMapper.getScheduleListBySemesteridTeacherY(userid, semesterid, platformid, experimentname, wlclassid, wlclassname, grade, begin, end, pagesize, timestamp);
                if (list.size() == 0) {
                    result.setMessage("没有已上排课！");
                    result.setResult(WebRestResult.FAILURE);
                    return result;
                }
            }
            if (list.size() > 0) {
                for (SchedulePageResult schedulePageResult : list) {

                    String sectionnum = schedulePageResult.getSectionnum();
                    String[] sectionarr = sectionnum.substring(sectionnum.indexOf('[') + 1, sectionnum.indexOf(']')).replaceAll(" ", "").split(",");
                    schedulePageResult.setSectionlist(Arrays.asList(sectionarr));
                    schedulePageResult.setSectionnumber(sectionarr.length);
                    List<WlClassResult> wlClassResults = scheduleMapper.getWlClassResults(schedulePageResult.getScheduleid());
                    schedulePageResult.setWlClassResults(wlClassResults);
                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    schedulePageResult.setCourseResults(courseResults);
//                  List<WlClassResult> wlClassResults = wlclassMapper.getWlClassResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    //根据班级id来得到排课id，因为之前返回的排课基本数据没有排课id，多个班级上一次相同的排课，会在数据库产生多条排课记录
                    //一个班级对应一条记录
//                    if (wlClassResults.size() > 0) {
////                        for (WlClassResult wlClassResult : wlClassResults) {
////                            String scheduleid = scheduleMapper.getScheduleidByWlclass(wlClassResult.getWlclassid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid(), schedulePageResult.getExperimentid());
////                            wlClassResult.setScheduleid(scheduleid);
////                        }
////                    }
////                    schedulePageResult.setWlClassResults(wlClassResults);
////                    List<TeacherResult> teacherResults = userMapper.getTeacherResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
////                    schedulePageResult.setTeacherResults(teacherResults);
////                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
////                    schedulePageResult.setCourseResults(courseResults);
                }
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setPageSize(pagesize);
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("老师查询排课成功！");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("老师没有排课信息！");
            }
            return result;
        } else if (userMapper.selectByPrimaryKey(userid).getRoleid().equals("3")) {
            Date date = new Date();
            Timestamp timestamp = null;
            List<SchedulePageResult> list = null;
            if ("0".equals(status)) {
                list = scheduleMapper.getScheduleListBySemesteridStudentN(userid, semesterid, platformid, experimentname, begin, end, pagesize, timestamp);
                if (list.size() == 0) {
                    result.setMessage("没有排课信息！");
                    result.setResult(WebRestResult.FAILURE);
                    return result;
                }
            } else if ("1".equals(status)) {
                timestamp = new Timestamp(date.getTime());
                list = scheduleMapper.getScheduleListBySemesteridStudentN(userid, semesterid, platformid, experimentname, begin, end, pagesize, timestamp);
                if (list.size() == 0) {
                    result.setMessage("没有未上排课！");
                    result.setResult(WebRestResult.FAILURE);
                    return result;
                }
            } else if ("2".equals(status)) {
                timestamp = new Timestamp(date.getTime());
                list = scheduleMapper.getScheduleListBySemesteridStudentY(userid, semesterid, platformid, experimentname, begin, end, pagesize, timestamp);
                if (list.size() == 0) {
                    result.setMessage("没有已上排课！");
                    result.setResult(WebRestResult.FAILURE);
                    return result;
                }
            }
            if (list.size() > 0) {
                for (SchedulePageResult schedulePageResult : list) {
                    String sectionnum = schedulePageResult.getSectionnum();
                    String[] sectionarr = sectionnum.substring(sectionnum.indexOf('[') + 1, sectionnum.indexOf(']')).replaceAll(" ", "").split(",");
                    schedulePageResult.setSectionlist(Arrays.asList(sectionarr));
                    schedulePageResult.setSectionnumber(sectionarr.length);
                    List<WlClassResult> wlClassResults = scheduleMapper.getWlClassResults(schedulePageResult.getScheduleid());
                    schedulePageResult.setWlClassResults(wlClassResults);
                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    schedulePageResult.setCourseResults(courseResults);
//                    List<WlClassResult> wlClassResults = wlclassMapper.getWlClassResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    //根据班级id来得到排课id，因为之前返回的排课基本数据没有排课id，多个班级上一次相同的排课，会在数据库产生多条排课记录
                    //一个班级对应一条记录
//                    if (wlClassResults.size() > 0) {
//                        for (WlClassResult wlClassResult : wlClassResults) {
//                            String scheduleid = scheduleMapper.getScheduleidByWlclass(wlClassResult.getWlclassid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid(), schedulePageResult.getExperimentid());
//                            wlClassResult.setScheduleid(scheduleid);
//                        }
//                    }
//                    schedulePageResult.setWlClassResults(wlClassResults);
//                    List<TeacherResult> teacherResults = userMapper.getTeacherResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setTeacherResults(teacherResults);
//                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setCourseResults(courseResults);
                }
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setPageSize(pagesize);
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("学生查询排课成功！");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("学生没有排课信息！");
            }
            return result;
        } else if (userMapper.selectByPrimaryKey(userid).getRoleid().equals("1")) {
            Date date = new Date();
            Timestamp timestamp = null;
            List<SchedulePageResult> list = null;
            if ("0".equals(status)) {
                list = scheduleMapper.getScheduleListBySemesteridAdminN(semesterid, platformid, experimentname, wlclassid, wlclassname, grade, begin, end, pagesize, timestamp);
                if (list.size() == 0) {
                    result.setMessage("没有排课信息！");
                    result.setResult(WebRestResult.FAILURE);
                    return result;
                }
            } else if ("1".equals(status)) {
                timestamp = new Timestamp(date.getTime());
                list = scheduleMapper.getScheduleListBySemesteridAdminN(semesterid, platformid, experimentname, wlclassid, wlclassname, grade, begin, end, pagesize, timestamp);
                if (list.size() == 0) {
                    result.setMessage("没有未上排课！");
                    result.setResult(WebRestResult.FAILURE);
                    return result;
                }
            } else if ("2".equals(status)) {
                timestamp = new Timestamp(date.getTime());
                list = scheduleMapper.getScheduleListBySemesteridAdminY(semesterid, platformid, experimentname, wlclassid, wlclassname, grade, begin, end, pagesize, timestamp);
                if (list.size() == 0) {
                    result.setMessage("没有已上排课！");
                    result.setResult(WebRestResult.FAILURE);
                    return result;
                }
            }
            if (list.size() > 0) {
                for (SchedulePageResult schedulePageResult : list) {
                    String sectionnum = schedulePageResult.getSectionnum();
                    String[] sectionarr = sectionnum.substring(sectionnum.indexOf('[') + 1, sectionnum.indexOf(']')).replaceAll(" ", "").split(",");
                    schedulePageResult.setSectionlist(Arrays.asList(sectionarr));
                    schedulePageResult.setSectionnumber(sectionarr.length);
                    List<WlClassResult> wlClassResults = scheduleMapper.getWlClassResults(schedulePageResult.getScheduleid());
                    schedulePageResult.setWlClassResults(wlClassResults);
                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    schedulePageResult.setCourseResults(courseResults);
//                    List<WlClassResult> wlClassResults = wlclassMapper.getWlClassResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    //根据班级id来得到排课id，因为之前返回的排课基本数据没有排课id，多个班级上一次相同的排课，会在数据库产生多条排课记录
                    //一个班级对应一条记录
//                    if (wlClassResults.size() > 0) {
//                        for (WlClassResult wlClassResult : wlClassResults) {
//                            String scheduleid = scheduleMapper.getScheduleidByWlclass(wlClassResult.getWlclassid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid(), schedulePageResult.getExperimentid());
//                            wlClassResult.setScheduleid(scheduleid);
//                        }
//                    }
//                    schedulePageResult.setWlClassResults(wlClassResults);
//                    List<TeacherResult> teacherResults = userMapper.getTeacherResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setTeacherResults(teacherResults);
//                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setCourseResults(courseResults);
                }
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setPageSize(pagesize);
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("管理员查询排课成功！");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("管理员查询排课失败或者没有排课！");
            }
            return result;
        }
        return result;
    }

    @Override
    public PageResult<List<SchedulePageResult>> getScheduleListByWeekUser(String userid, String weekdate) {
        PageResult<List<SchedulePageResult>> result = new PageResult<>();
        int begin = 0;
        int end = 1000;
        //字符串转日期
        String starttime = Tools.date2Str(Tools.getTheFirstDayOfWeek(Tools.str2Date1(weekdate)), "yyyy-MM-dd");
        String endtime = Tools.date2Str(new Date(Tools.str2Date1(starttime).getTime() + 7 * 24 * 60 * 60 * 1000), "yyyy-MM-dd");
        List<SchedulePageResult> list = null;
        if (userMapper.selectByPrimaryKey(userid).getRoleid().equals("2")) {
            list = scheduleMapper.getScheduleListByWeekTeacher(userid, begin, end, 1000, starttime, endtime);

            List<List<SchedulePageResult>> weekList = new ArrayList<>();
            for (int i = 1; i < 8; i++) {
                List<SchedulePageResult> tempList = new ArrayList<>();
                for (SchedulePageResult schedulePageResult : list) {
                    int weekDay = Tools.getWeekDay(Tools.str2Date(schedulePageResult.getStartdate()));
                    if (weekDay == i )
                        tempList.add(schedulePageResult);
                }
                weekList.add(tempList);
            }
            if (list.size() > 0) {
                for (SchedulePageResult schedulePageResult : list) {
                    String sectionnum = schedulePageResult.getSectionnum();
                    String[] sectionarr = sectionnum.substring(sectionnum.indexOf('[') + 1, sectionnum.indexOf(']')).replaceAll(" ", "").split(",");
                    schedulePageResult.setSectionlist(Arrays.asList(sectionarr));
                    schedulePageResult.setSectionnumber(sectionarr.length);
                    List<WlClassResult> wlClassResults = scheduleMapper.getWlClassResults(schedulePageResult.getScheduleid());
                    schedulePageResult.setWlClassResults(wlClassResults);
                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    schedulePageResult.setCourseResults(courseResults);
//                    List<WlClassResult> wlClassResults = wlclassMapper.getWlClassResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    //根据班级id来得到排课id，因为之前返回的排课基本数据没有排课id，多个班级上一次相同的排课，会在数据库产生多条排课记录
                    //一个班级对应一条记录
//                    if (wlClassResults.size() > 0) {
//                        for (WlClassResult wlClassResult : wlClassResults) {
//                            String scheduleid = scheduleMapper.getScheduleidByWlclass(wlClassResult.getWlclassid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid(), schedulePageResult.getExperimentid());
//                            wlClassResult.setScheduleid(scheduleid);
//                        }
//                    }
//                    schedulePageResult.setWlClassResults(wlClassResults);
//                    List<TeacherResult> teacherResults = userMapper.getTeacherResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setTeacherResults(teacherResults);
//                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setCourseResults(courseResults);
                }
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setRows(weekList);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("老师查询周排课成功！");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("老师这周没有课或查排课出错");
            }
            return result;
        } else if (userMapper.selectByPrimaryKey(userid).getRoleid().equals("3")) {
            list = scheduleMapper.getScheduleListByWeekStudent(userid, begin, end, 1000, starttime, endtime);

            List<List<SchedulePageResult>> weekList = new ArrayList<>();
            for (int i = 1; i < 8; i++) {
                List<SchedulePageResult> tempList = new ArrayList<>();
                for (SchedulePageResult schedulePageResult : list) {
                    int weekDay = Tools.getWeekDay(Tools.str2Date(schedulePageResult.getStartdate()));
                    if (weekDay == i )
                        tempList.add(schedulePageResult);
                }
                weekList.add(tempList);
            }
            if (list.size() > 0) {
                for (SchedulePageResult schedulePageResult : list) {
                    String sectionnum = schedulePageResult.getSectionnum();
                    String[] sectionarr = sectionnum.substring(sectionnum.indexOf('[') + 1, sectionnum.indexOf(']')).replaceAll(" ", "").split(",");
                    schedulePageResult.setSectionlist(Arrays.asList(sectionarr));
                    schedulePageResult.setSectionnumber(sectionarr.length);
                    List<WlClassResult> wlClassResults = scheduleMapper.getWlClassResults(schedulePageResult.getScheduleid());
                    schedulePageResult.setWlClassResults(wlClassResults);
                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    schedulePageResult.setCourseResults(courseResults);
//                    List<WlClassResult> wlClassResults = wlclassMapper.getWlClassResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    //根据班级id来得到排课id，因为之前返回的排课基本数据没有排课id，多个班级上一次相同的排课，会在数据库产生多条排课记录
                    //一个班级对应一条记录
//                    if (wlClassResults.size() > 0) {
//                        for (WlClassResult wlClassResult : wlClassResults) {
//                            String scheduleid = scheduleMapper.getScheduleidByWlclass(wlClassResult.getWlclassid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid(), schedulePageResult.getExperimentid());
//                            wlClassResult.setScheduleid(scheduleid);
//                        }
//                    }
//                    schedulePageResult.setWlClassResults(wlClassResults);
//                    List<TeacherResult> teacherResults = userMapper.getTeacherResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setTeacherResults(teacherResults);
//                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setCourseResults(courseResults);
                }
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setRows(weekList);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("学生查询周排课成功！");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("学生这周没有课或查排课出错");
            }
            return result;
        } else if (userMapper.selectByPrimaryKey(userid).getRoleid().equals("1")) {
            list = scheduleMapper.getScheduleListByWeekAdmin(begin, end, 1000, starttime, endtime);

            List<List<SchedulePageResult>> weekList = new ArrayList<>();
            for (int i = 1; i < 8; i++) {
                List<SchedulePageResult> tempList = new ArrayList<>();
                for (SchedulePageResult schedulePageResult : list) {
                    int weekDay = Tools.getWeekDay(Tools.str2Date(schedulePageResult.getStartdate()));
                    if (weekDay == i )
                        tempList.add(schedulePageResult);
                }
                weekList.add(tempList);
            }
            if (list.size() > 0) {
                for (SchedulePageResult schedulePageResult : list) {
                    String sectionnum = schedulePageResult.getSectionnum();
                    String[] sectionarr = sectionnum.substring(sectionnum.indexOf('[') + 1, sectionnum.indexOf(']')).replaceAll(" ", "").split(",");
                    schedulePageResult.setSectionlist(Arrays.asList(sectionarr));
                    schedulePageResult.setSectionnumber(sectionarr.length);
                    List<WlClassResult> wlClassResults = scheduleMapper.getWlClassResults(schedulePageResult.getScheduleid());
                    schedulePageResult.setWlClassResults(wlClassResults);
                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    schedulePageResult.setCourseResults(courseResults);
//                    List<WlClassResult> wlClassResults = wlclassMapper.getWlClassResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
                    //根据班级id来得到排课id，因为之前返回的排课基本数据没有排课id，多个班级上一次相同的排课，会在数据库产生多条排课记录
                    //一个班级对应一条记录
//                    if (wlClassResults.size() > 0) {
//                        for (WlClassResult wlClassResult : wlClassResults) {
//                            String scheduleid = scheduleMapper.getScheduleidByWlclass(wlClassResult.getWlclassid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid(), schedulePageResult.getExperimentid());
//                            wlClassResult.setScheduleid(scheduleid);
//                        }
//                    }
//                    schedulePageResult.setWlClassResults(wlClassResults);
//                    List<TeacherResult> teacherResults = userMapper.getTeacherResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setTeacherResults(teacherResults);
//                    List<CourseResult> courseResults = courseMapper.getCourseResults(schedulePageResult.getExperimentid(), schedulePageResult.getStartdate(), schedulePageResult.getSemesterid());
//                    schedulePageResult.setCourseResults(courseResults);
                }
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setRows(weekList);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("管理员查询周排课成功！");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("管理员这周没有课或查排课出错");
            }
            return result;
        }
        return result;
    }

    @Override
    public ScheduleResult getScheduleByid(String scheduleid) {
        ScheduleResult scheduleResult = scheduleMapper.getScheduleByid(scheduleid);
        if (scheduleResult != null) {
            String sectionnum = scheduleResult.getSectionnum();
            String[] sectionarr = sectionnum.substring(sectionnum.indexOf('[') + 1, sectionnum.indexOf(']')).replaceAll(" ", "").split(",");
            scheduleResult.setSectionlist(Arrays.asList(sectionarr));
            scheduleResult.setSectionnumber(sectionarr.length);
            List<WlClassResult> wlClassResults = scheduleMapper.getWlClassResults(scheduleResult.getScheduleid());
            scheduleResult.setWlClassResults(wlClassResults);
            List<CourseResult> courseResults = courseMapper.getCourseResults(scheduleResult.getExperimentid(), scheduleResult.getStartdate(), scheduleResult.getSemesterid());
            scheduleResult.setCourseResults(courseResults);
//            List<WlClassResult> wlClassResults = wlclassMapper.getWlClassResults(scheduleResult.getExperimentid(), scheduleResult.getStartdate(), scheduleResult.getSemesterid());
//            //根据班级id来得到排课id，因为之前返回的排课基本数据没有排课id，多个班级上一次相同的排课，会在数据库产生多条排课记录
//            //一个班级对应一条记录
//            if (wlClassResults.size() > 0) {
//                for (WlClassResult wlClassResult : wlClassResults) {
//                    String scheduleclassid = scheduleMapper.getScheduleidByWlclass(wlClassResult.getWlclassid(), scheduleResult.getStartdate(), scheduleResult.getSemesterid(), scheduleResult.getExperimentid());
//                    wlClassResult.setScheduleid(scheduleclassid);
//                }
//            }
//            scheduleResult.setWlClassResults(wlClassResults);
//            List<TeacherResult> teacherResults = userMapper.getTeacherResults(scheduleResult.getExperimentid(), scheduleResult.getStartdate(), scheduleResult.getSemesterid());
//            scheduleResult.setTeacherResults(teacherResults);
//            List<CourseResult> courseResults = courseMapper.getCourseResults(scheduleResult.getExperimentid(), scheduleResult.getStartdate(), scheduleResult.getSemesterid());
//            scheduleResult.setCourseResults(courseResults);
            scheduleResult.setResult(WebRestResult.SUCCESS);
            scheduleResult.setMessage("获取单个排课信息成功！");
            return scheduleResult;
        } else {
            scheduleResult.setResult(WebRestResult.FAILURE);
            scheduleResult.setMessage("获取单个排课信息失败");
            return scheduleResult;
        }
    }

    @Override
    public WebRestResult addSchedule(ScheduleDto scheduleDto) {
        WebRestResult result = new WebRestResult();

        if (scheduleDto == null) {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("请输入排课信息");
            return result;
        }

        if (scheduleDto.getWlclasslist() == null || scheduleDto.getWlclasslist().size() == 0) {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("必须选择至少一个排课的班级");
            return result;
        }
        if (scheduleDto.getTeacherid() == null) {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("必须选择一个排课的任课老师");
            return result;
        }
        //得到排课班级的总人数
        int curnums = 0;
        for (int i = 0; i < scheduleDto.getWlclasslist().size(); i++) {
            curnums += wlclassMapper.getStudentNumByClass(scheduleDto.getWlclasslist().get(i));
        }
        //一次排課多個班級放在關係表中
        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(scheduleDto, schedule);
        schedule.setScheduleid(UUIDUtil.randomUUID());

        List<String> sectionlist = scheduleDto.getSectionlist();
        String[] sectionStrarr = (String[]) sectionlist.toArray(new String[sectionlist.size()]);
        int[] sectionarr = Arrays.asList(sectionStrarr).stream().mapToInt(Integer::parseInt).toArray();
        Arrays.sort(sectionarr);
        //不能够排不连续节数的课
        if ((sectionarr[sectionarr.length - 1] - sectionarr[0] + 1 )!= sectionarr.length) {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("排一节课只能够排连续节数，不能够排不连续节数，避免课表显示不连续的节数的问题！");
            return result;
        }
        schedule.setSectionnum(Arrays.toString(sectionarr));
        String date = scheduleDto.getDate();
        //将date和sectionnum转为开始日期和结束日期
        Date startDate = Tools.getStartDateBySection(String.valueOf(sectionarr[0]), date);
        Date endDate = Tools.getEndDateBySection(String.valueOf(sectionarr[sectionarr.length - 1]), date);
        int isNotZero = scheduleMapper.getScheduleByTeacheridAndTime(scheduleDto.getTeacherid(), Tools.date2Str(startDate), Tools.date2Str(endDate));
        if (isNotZero != 0) {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("一个老师在同一时间只能排一次课！不能在相同时间给一个老师排多次课");
            return result;
        }

        schedule.setStartdate(startDate);
        schedule.setEnddate(endDate);
        String platformid = programMapper.getProgramPlatformById(scheduleDto.getPlatformid(), scheduleDto.getProgramid());
        String sycourseid = programMapper.getProgramSycourseById(scheduleDto.getPlatformid(), scheduleDto.getProgramid());
        if (platformid != null && sycourseid == null) {
            schedule.setPversion("1001");
        } else if (platformid == null && sycourseid != null) {
            schedule.setPversion("1002");
        } else {
            result.setMessage("根据平台或实验课程id来查询关系表没有找到对应的培养方案下的平台或实验课程！");
            result.setResult(WebRestResult.FAILURE);
            return result;
        }
        if (schedule.getMaxnums() == null) {
            schedule.setMaxnums(120);
        }
        schedule.setCurnums(curnums);
        schedule.setFlag("0");
        schedule.setCreatedTime(new Date());
        schedule.setUpdatedTime(new Date());
        List<ScheduleStudentGx> scheduleStudentGxList = new ArrayList<>();
        List<ScheduleWlclassVo> scheduleWlclassVoList = new ArrayList<>();
        for (String wlclassid : scheduleDto.getWlclasslist()) {
            if (scheduleMapper.getScheduleBywlclassidAndTime(wlclassid,  Tools.date2Str(startDate),  Tools.date2Str(endDate)) != 0) {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("一个班级在同一时间只能排一次课！不能在相同时间给一个班级排多次课");
                return result;
            }

            List<User> studentList = userMapper.getUserListByClassid(wlclassid);
            if (studentList.size() > 0) {
                for (int i = 0; i < studentList.size(); i++) {
                    ScheduleStudentGx scheduleStudentGx = new ScheduleStudentGx();
                    scheduleStudentGx.setStudentid(studentList.get(i).getUserid());
                    scheduleStudentGx.setScheduleid(schedule.getScheduleid());
                    scheduleStudentGx.setFlag("0");
                    scheduleStudentGx.setStatus("0");//0未做题
                    scheduleStudentGx.setCreatedTime(new Date());
                    scheduleStudentGx.setUpdatedTime(new Date());
                    scheduleStudentGxList.add(scheduleStudentGx);
                }
            }
            ScheduleWlclassVo scheduleWlclassVo = new ScheduleWlclassVo();
            scheduleWlclassVo.setScheduleid(schedule.getScheduleid());
            scheduleWlclassVo.setWlclassid(wlclassid);
            scheduleWlclassVoList.add(scheduleWlclassVo);
        }
        int flag = scheduleMapper.insertSelective(schedule);
        int gxFlag = scheduleStudentGxMapper.addGxList(scheduleStudentGxList);
        int swFlag = scheduleMapper.addScheduleWlclass(scheduleWlclassVoList);
        if (flag == 1 && gxFlag == scheduleStudentGxList.size() && swFlag== scheduleWlclassVoList.size()) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("新增排课成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增排课失败！");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
//        for (int i = 0; i < scheduleDto.getWlclasslist().size(); i++) {
//            Schedule schedule = new Schedule();
//            BeanUtils.copyProperties(scheduleDto, schedule);
//            schedule.setScheduleid(UUIDUtil.randomUUID());
//            schedule.setClassid(scheduleDto.getWlclasslist().get(i));
//            List<String> sectionlist = scheduleDto.getSectionlist();
//            schedule.setSectionnum(Arrays.toString(sectionlist.toArray()));
//            String date = scheduleDto.getDate();
//            //将date和sectionnum转为开始日期和结束日期
//            Date startDate = Tools.getStartDateBySection(sectionlist.get(0), date);
//            Date endDate = Tools.getEndDateBySection(sectionlist.get(sectionlist.size() - 1), date);
//            schedule.setStartdate(startDate);
//            schedule.setEnddate(endDate);
//
//            String platformid= programMapper.getProgramPlatformById(scheduleDto.getPlatformid(),scheduleDto.getProgramid());
//            String sycourseid= programMapper.getProgramSycourseById(scheduleDto.getPlatformid(),scheduleDto.getProgramid());
//            if (platformid != null && sycourseid == null) {
//                schedule.setPversion("1001");
//            } else if (platformid == null && sycourseid != null) {
//                schedule.setPversion("1002");
//            }else{
//                result.setMessage("根据平台或实验课程id来查询关系表没有找到对应的培养方案下的平台或实验课程！");
//                result.setResult(WebRestResult.FAILURE);
//                return result;
//            }
//            if (schedule.getMaxnums() == null) {
//                schedule.setMaxnums(120);
//            }
//            schedule.setCurnums(curnums);
//            schedule.setFlag("0");
//            schedule.setCreatedTime(new Date());
//            schedule.setUpdatedTime(new Date());
//            scheduleList.add(schedule);
//        }

//        for (Schedule schedule : scheduleList) {
//            String classid = schedule.getClassid();
//            List<User> studentList = userMapper.getUserListByClassid(classid);
//            if (studentList.size() > 0) {
//                for (int i = 0; i < studentList.size(); i++) {
//                    ScheduleStudentGx scheduleStudentGx = new ScheduleStudentGx();
//                    scheduleStudentGx.setStudentid(studentList.get(i).getUserid());
//                    scheduleStudentGx.setScheduleid(schedule.getScheduleid());
//                    scheduleStudentGx.setFlag("0");
//                    scheduleStudentGx.setCreatedTime(new Date());
//                    scheduleStudentGx.setUpdatedTime(new Date());
//                    scheduleStudentGxList.add(scheduleStudentGx);
//                }
//            }
//        }
    }

    @Override
    public PageResult<SchedulePageResult> getSemesterListHaveSchedule(int page, int pagesize, String semestername) {
        PageResult<SchedulePageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;

        List<SchedulePageResult> list = scheduleMapper.getSemesterListHaveSchedule(begin, end, pagesize, semestername);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setPageSize(pagesize);
            result.setRows(list);
            result.setMessage("获取已经有排课的学期列表成功！");
            result.setResult(WebRestResult.SUCCESS);
        } else {
            result.setMessage("没有已经有排课的学期列表！");
            result.setResult(WebRestResult.FAILURE);
        }
        return result;
    }

    @Override
    public PageResult<SemesterPageResult> getSemesterNoProgramList() {
        PageResult<SemesterPageResult> result = new PageResult<>();
        List<SemesterPageResult> list = scheduleMapper.getSemesterNoProgramList();
        if (list.size() > 0) {
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取未排课的学期列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("没有未排课的学期列表！");
        }
        return result;
    }

    @Override
    public WebRestResult addSemesterProgram(Schedule schedule) {
        WebRestResult result = new WebRestResult();
        schedule.setUpdatedTime(new Date());
        Program program = new Program();
        program.setProgramid(schedule.getProgramid());
        program.setUpdatedTime(new Date());
        program.setStatus("1");
        programMapper.updateByPrimaryKeySelective(program);
        if (scheduleMapper.addSemesterProgram(schedule) == 1) {
            result.setMessage("新增学期与培养方案的关系成功！");
            result.setResult(WebRestResult.SUCCESS);
        } else {
            result.setMessage("新增学期与培养方案的关系失败！");
            result.setResult(WebRestResult.FAILURE);
        }
        return result;
    }

    @Override
    public WebRestResult deleteSemesterProgram(String semesterid, String programid) {
        WebRestResult result = new WebRestResult();
        int n = scheduleMapper.getScheduleCountsbyId(semesterid, programid);
        if (n > 0) {
            result.setMessage("该排课版本还有"+n+"次排课，请先进入排课详情确认删除所有排课！");
            result.setResult(WebRestResult.FAILURE);
            return result;
        }
        if (scheduleMapper.deleteSemesterProgram(semesterid, programid) == 1) {
            List<SchedulePageResult> list = scheduleMapper.getSemesterProgram(0, 10000, 10000, null, null, programid);
            if (list.size() == 0) {
                Program program = new Program();
                program.setProgramid(programid);
                program.setStatus("0");
                programMapper.updateByPrimaryKeySelective(program);
            }
            result.setMessage("删除学期与培养方案的关系成功！");
            result.setResult(WebRestResult.SUCCESS);
        } else {
            result.setMessage("删除学期与培养方案的关系失败！");
            result.setResult(WebRestResult.FAILURE);
        }
        return result;
    }

    @Override
    public WebRestResult updateSemesterProgram(String semesterid, String programid) {
        WebRestResult result = new WebRestResult();
        if (scheduleMapper.updateSemesterProgram(semesterid, programid, new Date()) == 1) {
            result.setMessage("更新学期与培养方案的关系成功！");
            result.setResult(WebRestResult.SUCCESS);
        } else {
            result.setMessage("更新学期与培养方案的关系失败！");
            result.setResult(WebRestResult.FAILURE);
        }
        return result;
    }

    @Override
    public PageResult<SchedulePageResult> getSemesterProgram(int page, int pagesize, String semestername, String programname) {
        PageResult<SchedulePageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;

        List<SchedulePageResult> list = scheduleMapper.getSemesterProgram(begin, end, pagesize, semestername, programname, null);
        if (list.size() > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setRows(list);
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setMessage("获取学期与培养方案关系成功");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("没有获取学期与培养方案的关系");
        }
        return result;
    }

    @Override
    public ScheduleSemesterResult getAllSchedulebySemester(String userid, String semesterid) {
        ScheduleSemesterResult result = new ScheduleSemesterResult();
        String roleid = userMapper.selectByPrimaryKey(userid).getRoleid();
        SemesterResult semester = semesterMapper.getSemesterById(semesterid);
        //根据学期开始日期和学期的周数计算该学期内第几周星期几是几月几号
        List<ScheduleWeekResult> list = new ArrayList<>();
        Date startdate = semester.getStartdate();
        int weeknum = semester.getWeeknum();
        //老师端
        if ("2".equals(roleid)) {
            List<SchedulePageResult> schedulePageResults = scheduleMapper.getScheduleListBySemesteridTeacherN(userid, semesterid, null, null, null, null, null, 0, 100000, 100000, null);

            result.setSemesterid(semester.getSemesterid());
            result.setSemestername(semester.getSemestername());
            result.setWeeknum(weeknum);
            result.setStartdate(Tools.date2Str(startdate));
            result.setSchedulelist(list);
            Date mon = Tools.getThisWeekMonday(startdate);
            Date sun = Tools.getThisWeekSunday(startdate);
            for (int i = 0; i < weeknum; i++) {
                Date monday = new Date(mon.getTime() + (i * 7 * 24 * 60 * 60 * 1000L));
                Date sunday = new Date(sun.getTime() + (i * 7 * 24 * 60 * 60 * 1000L));
                String begin = Tools.date2Str(monday, "yyyy-MM-dd");
                String end = Tools.date2Str(sunday, "yyyy-MM-dd");
                ScheduleWeekResult weekResult = new ScheduleWeekResult();
                weekResult.setWeek(i + 1);
                weekResult.setMonday(begin);
                weekResult.setSunday(end);
                List<String> experimentlist = new ArrayList<>();
                begin += " 00:00:00";
                end += " 23:59:59";
                for (SchedulePageResult schedulePageResult : schedulePageResults) {
                    String resultStartdate = schedulePageResult.getStartdate();
                    String resultEnddate = schedulePageResult.getEnddate();
                    if (begin.compareTo(resultStartdate) < 0 && end.compareTo(resultEnddate) > 0) {
                        experimentlist.add(schedulePageResult.getExperimentname() + " " + schedulePageResult.getStartdate() + "~" + Tools.date2Str(Tools.str2Date(schedulePageResult.getEnddate()),"HH:mm:ss"));
                    }
                }
                weekResult.setExperimentlist(experimentlist);
                list.add(weekResult);
            }
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("老师端根据学期获取总课表成功！");
            return result;
            //学生端
        }else if ("3".equals(roleid)) {
            List<SchedulePageResult> schedulePageResults = scheduleMapper.getScheduleListBySemesteridStudentN(userid, semesterid, null, null, 0, 10000, 10000, null);

            result.setSemesterid(semester.getSemesterid());
            result.setSemestername(semester.getSemestername());
            result.setWeeknum(weeknum);
            result.setStartdate(Tools.date2Str(startdate));
            result.setSchedulelist(list);
            Date mon = Tools.getThisWeekMonday(startdate);
            Date sun = Tools.getThisWeekSunday(startdate);
            for (int i = 0; i < weeknum; i++) {
                Date monday = new Date(mon.getTime() + (i * 7 * 24 * 60 * 60 * 1000L));
                Date sunday = new Date(sun.getTime() + (i * 7 * 24 * 60 * 60 * 1000L));
                String begin = Tools.date2Str(monday, "yyyy-MM-dd");
                String end = Tools.date2Str(sunday, "yyyy-MM-dd");
                ScheduleWeekResult weekResult = new ScheduleWeekResult();
                weekResult.setWeek(i + 1);
                weekResult.setMonday(begin);
                weekResult.setSunday(end);
                List<String> experimentlist = new ArrayList<>();
                begin += " 00:00:00";
                end += " 23:59:59";
                for (SchedulePageResult schedulePageResult : schedulePageResults) {
                    String resultStartdate = schedulePageResult.getStartdate();
                    String resultEnddate = schedulePageResult.getEnddate();
                    if (begin.compareTo(resultStartdate) < 0 && end.compareTo(resultEnddate) > 0) {
                        experimentlist.add(schedulePageResult.getExperimentname() + " " + schedulePageResult.getStartdate() + "~" + Tools.date2Str(Tools.str2Date(schedulePageResult.getEnddate()),"HH:mm:ss"));
                    }
                }
                weekResult.setExperimentlist(experimentlist);
                list.add(weekResult);
            }
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("学生端根据学期获取总课表成功！");
            return result;
            //管理员端
        }else if ("1".equals(roleid)) {
            List<SchedulePageResult> schedulePageResults = scheduleMapper.getScheduleListBySemesteridAdminN(semesterid, null, null, null, null, null, 0, 100000, 100000, null);
            result.setSemesterid(semester.getSemesterid());
            result.setSemestername(semester.getSemestername());
            result.setWeeknum(weeknum);
            result.setStartdate(Tools.date2Str(startdate));
            result.setSchedulelist(list);
            Date mon = Tools.getThisWeekMonday(startdate);
            Date sun = Tools.getThisWeekSunday(startdate);
            for (int i = 0; i < weeknum; i++) {
                Date monday = new Date(mon.getTime() + (i * 7 * 24 * 60 * 60 * 1000L));
                Date sunday = new Date(sun.getTime() + (i * 7 * 24 * 60 * 60 * 1000L));
                String begin = Tools.date2Str(monday, "yyyy-MM-dd");
                String end = Tools.date2Str(sunday, "yyyy-MM-dd");
                ScheduleWeekResult weekResult = new ScheduleWeekResult();
                weekResult.setWeek(i + 1);
                weekResult.setMonday(begin);
                weekResult.setSunday(end);
                List<String> experimentlist = new ArrayList<>();
                begin += " 00:00:00";
                end += " 23:59:59";
                for (SchedulePageResult schedulePageResult : schedulePageResults) {
                    String resultStartdate = schedulePageResult.getStartdate();
                    String resultEnddate = schedulePageResult.getEnddate();
                    if (begin.compareTo(resultStartdate) < 0 && end.compareTo(resultEnddate) > 0) {
                        experimentlist.add(schedulePageResult.getExperimentname() + " " + schedulePageResult.getStartdate() + "~" + Tools.date2Str(Tools.str2Date(schedulePageResult.getEnddate()),"HH:mm:ss"));
                    }
                }
                weekResult.setExperimentlist(experimentlist);
                list.add(weekResult);
            }
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("管理员端根据学期获取总课表成功！");
            return result;
        }
        return result;
    }

    @Override
    public PageResult<ScheduleRemindResult> getScheduleRemindByUser(String userid) {
        PageResult<ScheduleRemindResult> result = new PageResult<>();
        User user = userMapper.selectByPrimaryKey(userid);
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);//明天
        Date tomorrow = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, +2);//后天
        Date aftertomorrow = calendar.getTime();
        String tomorrowstr = Tools.date2Str(tomorrow, "yyyy-MM-dd");
        String aftertomorrowstr = Tools.date2Str(aftertomorrow, "yyyy-MM-dd");
        ArrayList<ScheduleRemindResult> list = new ArrayList<>();
        if (user.getRoleid().equals("2")) {
            List<SchedulePageResult> schedulePageResults = scheduleMapper.getScheduleListByWeekTeacher(userid, 0, 10000, 10000, tomorrowstr, aftertomorrowstr);
            for (SchedulePageResult schedulePageResult : schedulePageResults) {
                ScheduleRemindResult scheduleRemindResult = new ScheduleRemindResult();
                scheduleRemindResult.setExperimentname(schedulePageResult.getExperimentname());
                scheduleRemindResult.setExperimentroom(schedulePageResult.getExperimentroom());
                scheduleRemindResult.setTeachername(schedulePageResult.getTeachername());
                scheduleRemindResult.setTime(schedulePageResult.getStartdate() + "" + Tools.date2Str(Tools.str2Date(schedulePageResult.getEnddate()), "HH:mm:ss"));
                list.add(scheduleRemindResult);
            }
            if (list.size() > 0) {
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("老师获取明日提醒列表信息成功！");
                result.setTotal(list.get(0).getCnt());
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("老师获取明日提醒列表信息为空！");
                result.setTotal(0);
            }
            return result;
        }else if (user.getRoleid().equals("3")) {
            List<SchedulePageResult> schedulePageResults = scheduleMapper.getScheduleListByWeekStudent(userid, 0, 10000, 10000, tomorrowstr, aftertomorrowstr);
            for (SchedulePageResult schedulePageResult : schedulePageResults) {
                ScheduleRemindResult scheduleRemindResult = new ScheduleRemindResult();
                scheduleRemindResult.setExperimentname(schedulePageResult.getExperimentname());
                scheduleRemindResult.setExperimentroom(schedulePageResult.getExperimentroom());
                scheduleRemindResult.setTeachername(schedulePageResult.getTeachername());
                scheduleRemindResult.setTime(schedulePageResult.getStartdate() + "" + Tools.date2Str(Tools.str2Date(schedulePageResult.getEnddate()), "HH:mm:ss"));
                list.add(scheduleRemindResult);
            }
            if (list.size() > 0) {
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("学生获取明日提醒列表信息成功！");
                result.setTotal(list.get(0).getCnt());
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("学生获取明日提醒列表信息为空！");
                result.setTotal(0);
            }
            return result;
        }else if (user.getRoleid().equals("1")) {
            List<SchedulePageResult> schedulePageResults = scheduleMapper.getScheduleListByWeekStudent(userid, 0, 10000, 10000, tomorrowstr, aftertomorrowstr);
            for (SchedulePageResult schedulePageResult : schedulePageResults) {
                ScheduleRemindResult scheduleRemindResult = new ScheduleRemindResult();
                scheduleRemindResult.setExperimentname(schedulePageResult.getExperimentname());
                scheduleRemindResult.setExperimentroom(schedulePageResult.getExperimentroom());
                scheduleRemindResult.setTeachername(schedulePageResult.getTeachername());
                scheduleRemindResult.setTime(schedulePageResult.getStartdate() + "" + Tools.date2Str(Tools.str2Date(schedulePageResult.getEnddate()), "HH:mm:ss"));
                list.add(scheduleRemindResult);
            }
            if (list.size() > 0) {
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("管理员获取明日提醒列表信息成功！");
                result.setTotal(list.get(0).getCnt());
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("管理员获取明日提醒列表信息为空！");
                result.setTotal(0);
            }
            return result;
        }
        return result;
    }

    @Override
    public ScheduleSafetynoticeResult getSafetynoticeByScheduleid(String scheduleid) {
        ScheduleSafetynoticeResult result = scheduleMapper.getSafetynoticeByScheduleid(scheduleid);
        if (result != null) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("根据排课id获取安全须知成功！");
        }
        return result;
    }
}

